#!/bin/bash

# scriptPath=$(cd $(dirname $0) && pwd)
# cd ${scriptPath}

function messagebox() {
    # messagebox <title> [<content> [<height> [<width>]]]
    title=$1
    msgbox=$2
    height=10
    width=60
    ok_button="Ok"
    if [ -n "$3" ]; then
        height=$3
    fi
    if [ -n "$4" ]; then
        width=$4
    fi
    if [ -n "$5" ]; then
        ok_button=$5
    fi
    # 消息框
    # whiptail --title "<message box title>" --msgbox "<text to show>" <height> <width>
    # whiptail --title "Test Message Box" --msgbox "Create a message box with whiptail. Choose Ok to continue." 10 60
    whiptail --title "$title" --ok-button "$ok_button" --msgbox "$msgbox" $height $width
}

# messagebox "向导" "开始配置向导" 10 60 "开始"

function promptbox() {
    # promptbox <title> [<content> [<height> [<width> [<yes_button> [<no_button>]]]]]
    title=$1
    yesno=$2
    height=10
    width=60
    yes_button="Yes"
    no_button="No"
    if [ -n "$3" ]; then
        height=$3
    fi
    if [ -n "$4" ]; then
        width=$4
    fi
    if [ -n "$5" ]; then
        yes_button=$5
    fi
    if [ -n "$6" ]; then
        no_button=$6
    fi

    # 提示框
    # whiptail --title "<dialog box title>" --yesno "<text to show>" <height> <width>
    #!/bin/bash if (whiptail --title "Test Yes/No Box" --yes-button "Skittles" --no-button "M&M's" --yesno "Which do you like better?" 10 60) then echo "You chose Skittles Exit status was $?." else echo "You chose M&M's. Exit status was $?." fi
    whiptail --title "$title" --yes-button "$yes_button" --no-button "$no_button" --yesno "$yesno" $height $width
    ret=$?
    return $ret
}

# promptbox "提示" "是否提交您的信息" 10 60 "是" "否"
# ret=$?
# echo $ret

function inputbox() {
    # inputbox <title> [<content> [<height> [<width> [<default>]]]]
    title=$1
    inputbox=$2
    height=10
    width=60
    default=$5
    ok_button="Ok"
    cancel_button="Canncel"
    if [ -n "$3" ]; then
        height=$3
    fi
    if [ -n "$4" ]; then
        width=$4
    fi
    if [ -n "$6" ]; then
        ok_button=$6
    fi
    if [ -n "$7" ]; then
        cancel_button=$7
    fi
    # 输入框
    # whiptail --title "<input box title>" --inputbox "<text to show>" <height> <width> <default-text>
    #!/bin/bash PET=$(whiptail --title "Test Free-form Input Box" --inputbox "What is your pet's name?" 10 60 Wigglebutt 3>&1 1>&2 2>&3) exitstatus=$? if [ $exitstatus = 0 ]; then echo "Your pet name is:" $PET else echo "You chose Cancel." fi
    value=$(whiptail --title "$title" --ok-button "$ok_button" --cancel-button "$cancel_button" --inputbox "$inputbox" $height $width "$default" 3>&1 1>&2 2>&3)
    exitstatus=$?
    # echo "exitstatus:$exitstatus"
    if [ $exitstatus = 0 ]; then
        echo "$value"
    fi
    return $((10#${exitstatus}))

}

# value=$(inputbox "联系方式" "输入您的联系方式" 10 60 "默认内容")
# ret=$?
# echo $value
# echo $ret

function passwordbox() {
    # inputbox <title> [<content> [<height> [<width> [<default>]]]]
    title=$1
    inputbox=$2
    height=10
    width=60
    default=$5
    ok_button="Ok"
    cancel_button="Canncel"
    if [ -n "$3" ]; then
        height=$3
    fi
    if [ -n "$4" ]; then
        width=$4
    fi
    if [ -n "$6" ]; then
        ok_button=$6
    fi
    if [ -n "$7" ]; then
        cancel_button=$7
    fi

    # 密码输入框
    # whiptail --title "<password box title>" --passwordbox "<text to show>" <height> <width>
    #!/bin/bash PASSWORD=$(whiptail --title "Test Password Box" --passwordbox "Enter your password and choose Ok to continue." 10 60 3>&1 1>&2 2>&3) exitstatus=$? if [ $exitstatus = 0 ]; then echo "Your password is:" $PASSWORD else echo "You chose Cancel." fi

    value=$(whiptail --title "$title" --ok-button "$ok_button" --cancel-button "$cancel_button" --passwordbox "$inputbox" $height $width "$default" 3>&1 1>&2 2>&3)
    exitstatus=$?
    # echo "exitstatus:$exitstatus"
    if [ $exitstatus = 0 ]; then
        echo "$value"
    fi
    return $((10#${exitstatus}))
}

# value=$(passwordbox "登录密码" "请输入您的登录免密" 10 60 "admin")
# ret=$?
# echo $value
# echo $ret

# 菜单栏
# whiptail --title "<menu title>" --menu "<text to show>" <height> <width> <menu height> [ <tag> <item> ] . . .
#!/bin/bash OPTION=$(whiptail --title "Test Menu Dialog" --menu "Choose your option" 15 60 4 \ "1" "Grilled Spicy Sausage" \ "2" "Grilled Halloumi Cheese" \ "3" "Charcoaled Chicken Wings" \ "4" "Fried Aubergine" 3>&1 1>&2 2>&3) exitstatus=$? if [ $exitstatus = 0 ]; then echo "Your chosen option:" $OPTION else echo "You chose Cancel." fi

# 单选框
# whiptail --title "<radiolist title>" --radiolist "<text to show>" <height> <width> <list height> [ <tag> <item> <status> ] . . .
#!/bin/bash DISTROS=$(whiptail --title "Test Checklist Dialog" --radiolist \ "What is the Linux distro of your choice?" 15 60 4 \ "debian" "Venerable Debian" ON \ "ubuntu" "Popular Ubuntu" OFF \ "centos" "Stable CentOS" OFF \ "mint" "Rising Star Mint" OFF 3>&1 1>&2 2>&3) exitstatus=$? if [ $exitstatus = 0 ]; then echo "The chosen distro is:" $DISTROS else echo "You chose Cancel." fi

# 多选框
# whiptail --title "<checklist title>" --checklist "<text to show>" <height> <width> <list height> [ <tag> <item> <status> ] . . .
#!/bin/bash DISTROS=$(whiptail --title "Test Checklist Dialog" --checklist \ "Choose preferred Linux distros" 15 60 4 \ "debian" "Venerable Debian" ON \ "ubuntu" "Popular Ubuntu" OFF \ "centos" "Stable CentOS" ON \ "mint" "Rising Star Mint" OFF 3>&1 1>&2 2>&3) exitstatus=$? if [ $exitstatus = 0 ]; then echo "Your favorite distros are:" $DISTROS else echo "You chose Cancel." fi

# 进度条
# whiptail --gauge "<test to show>" <height> <width> <inital percent>
#!/bin/bash { for ((i = 0 ; i <= 100 ; i+=20)); do sleep 1 echo $i done } | whiptail --gauge "Please wait while installing" 6 60 0
